package com.zb_study.smallchange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeSys {


    public static void main(String[] args) {

        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key = "";

        double money = 0;
        double balance = 0;

        String place = "";

        String ex = "";

        Date date = null; //date 是 java.util.Date 类型，表示日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        String details = "-------------零钱通明细--------------";

        do {
            System.out.println("=================零钱通菜单=================");
            System.out.println("\t\t\t1 零钱通明细");
            System.out.println("\t\t\t2 收益入账");
            System.out.println("\t\t\t3 消费");
            System.out.println("\t\t\t4 退     出");
            System.out.print("请选择(1-4)：");

            key = scanner.next();

            switch (key) {
                case "1":
                    System.out.println(details);
                    break;
                case "2":
                    System.out.print("收益入账金额：");
                    money = scanner.nextDouble();
                    if (money <= 0) {
                        System.out.println("收益入账金额应该大于 0 元");
                        break;
                    }
                    balance += money;
                    date = new Date(); //获取当前日期
                    // System.out.println(sdf.format(date));
                    details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;
                case "3":
                    System.out.print("输入消费金额：");
                    money = scanner.nextDouble();
                    if (money <= 0 || money > balance) {
                        System.out.println("消费金额应该在0-" + balance + "之间");
                        break;
                    }
                    System.out.print("输入消费事物：");
                    place = scanner.next();
                    balance -= money;
                    details += "\n" + place + "\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;
                case "4":
                    System.out.println("你确定要退出吗？y/n");
                    while (true) {
                        ex = scanner.next();
                        if (ex.equals("y")) {
                            loop = false;
                            break;
                        } else if (ex.equals("n")) {
                            break;
                        } else {
                            System.out.println("输入错误，请重新输入(y/n)：");
                        }
                    }
                    break;

                default:
                    System.out.println("输入错误，请重新输入(1-4):");

            }
        } while (loop);

        System.out.println("------------退出零钱通系统------------");

    }
}
